var UUID = function(mostSigBits,leastSigBits){
	// setup hash field

	this.mostSigBits =mostSigBits;
	this.leastSigBits = leastSigBits;
    this.toString= function() {
        var builder = "";
        var msbStr = ""+parseInt(this.mostSigBits,16).toString();
        // console.log(msbStr);
        if (msbStr.length < 16) {
            var  diff = 16 - msbStr.length;
            // console.log(diff);
            for (var i = 0; i < diff; i++) {
                builder=builder+"0";
            }
        }
        builder+=msbStr;
        // console.log(builder)
        builder = insertNumber(builder,8,'-');
        // console.log("8888:"+builder)
        builder = insertNumber(builder,13,'-');
        // builder.insert(8, '-');
        // builder.insert(13, '-');
        builder+='-';
        
        var  lsbStr = parseInt(this.leastSigBits,16).toString();
        if (lsbStr.length < 16) {
            var diff = 16 - lsbStr.length;
            for (var i = 0; i < diff; i++) {
                builder+="0";
            }
        }
        console.log(lsbStr)
        //builder.append(lsbStr);
        builder+=lsbStr;
        // builder.insert(23, '-');
        console.log(builder)
        builder = insertNumber(builder,23,'-');
        console.log(builder)
        return builder;
    }
    return this;
}


function getDeviceId(ctx) {
	var  tm = ctx
	var tmDevice, tmSerial, androidId; 
	tmDevice = tm.tmDevice; 
	tmSerial = tm.tmSerial; 
	androidId = tm.androidId;
    var deviceUuid = new UUID(androidId.hashCode(),(tmDevice.hashCode()<<32)|tmSerial.hashCode());
    deviceId = deviceUuid.toString();
    return deviceId;
}
//插入字符
function insertNumber(mainStr,searchNum,insertStr) {
    var front=mainStr.slice(0,searchNum);
    var end=mainStr.slice(searchNum,-1);
    if(front!=null && end!=null){
       return front+insertStr+end;
    }
    return null;
}
 
//js版hash函数
String.prototype.hashCode = function(){
	var hash = 0;
	if (this.length == 0) return hash;
	for (i = 0; i < this.length; i++) {
		char = this.charCodeAt(i);
		hash = ((hash<<5)-hash)+char;
		hash = hash & hash; // Convert to 32bit integer
	}
	return hash;
}

 // 超级跳跳:
// tmDevice ＝ 869718027590691
// tmSerial ＝ 89860316940214012038
// androidId ＝ 2c98099a5ec91714
// deviceId ＝ 00000000-7c83-9230-ffff-ffffe4f0e70b
// 超级跳跳:
// tmDevice ＝ 359596061708628
// tmSerial ＝ null
// androidId ＝ 39f3c8d3df84cc11
// deviceId ＝ 00000000-00ba-9eb5-41fd-188d0033c587
var deviceId = null;
var datas={'tmDevice':'869718027590691','tmSerial':'89860316940214012038','androidId':'2c98099a5ec91714'};

$('.aa').html(getDeviceId(datas));